package com.norriekoelle.virtualsensors.client;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.norriekoelle.virtualsensors.R;
import com.norriekoelle.virtualsensors.shared.BookshelfObject;

/**
 * The adapter for the {@link BookDatabaseHelper}.
 *
 * NOTE: If the database is changed, uninstall the app before reinstalling.
 * 
 * @author Marion Koelle (marionkoelle)
 */
public class BookDatabaseAdapter {
	
	public static enum DatabaseFields {
		ROWID("_id"),
		DRAWABLE("drawable"),
		TITLE("title"), 
		URL("url"),
		TYPE("type"),
		AUTHOR("author"),
		COLLECTION("collection");
		
		private String key;
		
		private DatabaseFields(String key){
			this.key = key;
		}
		
		public String getKey() {
			return key;
		}
	};
	public enum ObjectType {
		BOOK, PAPER, VIDEO, BOOK_FULL_TEXTURE, UNKNOWN;
	}
	public enum Collection {
		ALL("All"), INFERENCE("Inference"), DYNAMICS("Dynamics"), INTERACTION("Interaction");
		
		private String name;

		private Collection(String name) {
			this.name = name;
		}
		
		public String getName() {
			return name;
		}
	}	
	
	private static String[] names = {"ALL", "INFERENCE", "DYNAMICS", "INTERACTION"};
	
	public static String[] getCollectionNames() {
		if (names == null) {
			int collectionCount = Collection.values().length;
			String[] names = new String[collectionCount];
			for (int i=0; i < collectionCount; i++) {
				String name = Collection.values()[i].name().toLowerCase(); 
				names[i] = name;
			}
		}
		return names;
	}
	
	private static final String DATABASE_TABLE = "books";
	private static final String LOG = "LOG";
	private Context context;
	private SQLiteDatabase database;
	private BookDatabaseHelper dbHelper;

	public BookDatabaseAdapter(Context context) {
		this.context = context;
	}
	
	public String[] books = {
			"http://www.dcs.gla.ac.uk/~norrielm/documents/Norrie-MurraySmith-VirtualSensors.pdf",
	};
	
	/**
	 * Return a new database helper populated with test data.
	 */
 	public BookDatabaseAdapter open() throws SQLException {
 		dbHelper = new BookDatabaseHelper(context);
 		// TODO(marionkoelle): enable insertion via userinput
 		
 		/*
 		 * See http://www.librarything.com/catalog/rodms/yourlibrary for more tech books.
 		 */
 		
 		insertBook("http://images.amazon.com/images/P/0262600471.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Dynamics in Action", 
				" Alicia Juarrero", 
				"http://images.amazon.com/images/P/0262600471.01._SX200_SY250_SCLZZZZZZZ_.jpg", ObjectType.BOOK, 
				Collection.DYNAMICS);
 		insertBook("http://images.amazon.com/images/P/0262720477.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Democratizing Innovation", 
				"Eric Von Hippel", 
				"", ObjectType.BOOK, 
				Collection.INFERENCE);
 		insertBook("http://images.amazon.com/images/P/0521536685.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Subjective Probability", 
				"Richard Jeffrey", 
				"http://images.amazon.com/images/P/0521536685.01._SX200_SY250_SCLZZZZZZZ_.jpg", ObjectType.BOOK, 
				Collection.INFERENCE);
 		insertBook("http://images.amazon.com/images/P/0743276639.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"The Emotion Machine", 
				"Marvin Minsky", 
				"http://www.librarything.com/work/book/26467429", ObjectType.BOOK, 
				Collection.DYNAMICS);
 		insertBook("http://images.amazon.com/images/P/0132065509.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Information Visualization: Design for Interaction", 
				"Robert Spence", 
				"", ObjectType.BOOK, 
				Collection.INTERACTION);
 		insertBook("http://images.amazon.com/images/P/0333735277.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"The Mathematical Brain", 
				"Brian Butterworth", 
				"", ObjectType.BOOK, 
				Collection.INFERENCE);
 		insertBook("http://images.amazon.com/images/P/0521642981.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Information Theory, Inference and Learning Algorithms", 
				"David J. C. MacKay", 
				"", ObjectType.BOOK, 
				Collection.DYNAMICS);
 		insertBook("http://pics.librarything.com/picsizes/72/9e/729ead6ab9706bb593359415351444941506f41.jpg", 
				"Living control systems", 
				"William T. Powers", 
				"", ObjectType.BOOK, 
				Collection.INTERACTION);
 		insertBook("http://images.amazon.com/images/P/0130224642.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Linear Estimation", 
				"Thomas Kailath", 
				"", ObjectType.BOOK, 
				Collection.INTERACTION);
 		insertBook("http://images.amazon.com/images/P/0471948381.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Decision and Control", 
				"Stafford Beer", 
				"", ObjectType.BOOK, 
				Collection.DYNAMICS);
 		insertBook("http://pics.librarything.com/picsizes/e5/6f/e56f5d0987a779b593057485251444941506f41.jpg", 
				"Economics & The Public Purpose", 
				"John Kenneth Galbraith", 
				"", ObjectType.BOOK, 
				Collection.INFERENCE);
 		
 		insertBook("http://images.amazon.com/images/P/0679412263.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"The Evolution of Useful Things", 
				"Henry Petroski", 
				"", ObjectType.BOOK, 
				Collection.INTERACTION);
 		insertBook("http://pics.librarything.com/picsizes/e8/60/e860554fa0a0412597832484e41444941506f41.jpg", 
				"Intellectuals", 
				"Paul Johnson", 
				"", ObjectType.BOOK, 
				Collection.INFERENCE);
 		insertBook("http://images.amazon.com/images/P/0970470177.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"More Mind Readings", 
				"Richard S. Marken", 
				"", ObjectType.BOOK, 
				Collection.DYNAMICS);
 		insertBook("http://images.amazon.com/images/P/0850668506.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Robotics, Control And Society", 
				"N Moray", 
				"", ObjectType.BOOK, 
				Collection.INTERACTION);
 		insertBook("http://images.amazon.com/images/P/0130224642.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Linear Estimation", 
				"Thomas Kailath", 
				"", ObjectType.BOOK, 
				Collection.DYNAMICS);
 		insertBook("http://images.amazon.com/images/P/0099427753.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Interface", 
				"Neal Stephenson", 
				"", ObjectType.BOOK, 
				Collection.INFERENCE);
 		// 11
		// Inference (Physical)
 		insertBook("http://images.amazon.com/images/P/0130151572.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/speech_recognition_coverscan.png", 
				context.getResources().getString(R.string.speech_recognition), 
				context.getResources().getString(R.string.speech_recognition_author), 
				"http://www.librarything.com/work/1538391", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://images.amazon.com/images/P/1849963312.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/social_media_coverscan.png", 
				context.getResources().getString(R.string.social_media), 
				context.getResources().getString(R.string.social_media_author), 
				"http://www.librarything.com/work/10888370", ObjectType.BOOK, 
				Collection.DYNAMICS);
		insertBook("http://images.amazon.com/images/P/0521865719.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/information_retrieval_coverscan.png", 
				context.getResources().getString(R.string.introduction_information_retrieval), 
				context.getResources().getString(R.string.introduction_information_retrieval_author), 
				"http://www.librarything.com/work/4280462", ObjectType.BOOK, 
				Collection.DYNAMICS);
		// Inference (Virtual)
		insertBook("http://bks7.books.google.co.uk/books?id=az_Oi9mSSiIC&printsec=frontcover&img=1&zoom=1", 
				context.getResources().getString(R.string.game_engine_architecture),
				context.getResources().getString(R.string.game_engine_architecture_author), 
				"http://www.librarything.com/work/8999424", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://bks5.books.google.co.uk/books?id=btpIkZ6X6egC&printsec=frontcover&img=1&zoom=1", 
				context.getResources().getString(R.string.game_of_thrones), 
				context.getResources().getString(R.string.game_of_thrones_author), 
				"http://www.librarything.com/work/8366", ObjectType.BOOK, 
				Collection.INTERACTION);
		insertBook("http://images.amazon.com/images/P/1558604545.01._SX140_SY225_SCLZZZZZZZ_.jpg", 
				context.getResources().getString(R.string.information_retrieval), 
				context.getResources().getString(R.string.information_retrieval_author), 
				"http://www.librarything.com/work/294323", ObjectType.BOOK, 
				Collection.DYNAMICS);
		insertBook("http://images.amazon.com/images/P/0596514557.01._SX140_SY225_SCLZZZZZZZ_.jpg", 
				context.getResources().getString(R.string.visualizing_data), 
				context.getResources().getString(R.string.visualizing_data_author), 
				"http://www.librarything.com/work/4108432/book/77792770", ObjectType.BOOK, 
				Collection.INTERACTION);
		// Additionals
		insertBook("http://images.amazon.com/images/P/0571225233.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Red Plenty", 
				" Francis Spufford", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.DYNAMICS);
		insertBook("http://images.amazon.com/images/P/0596805888.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Cooking for Geeks: Real Science, Great Hacks, and Good Food", 
				"Jeff Potter", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://images.amazon.com/images/P/3899552172.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Data Flow: Visualising Information in Graphic Design", 
				"R. Klanten", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://images.amazon.com/images/P/0349116059.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"The Wisdom of Crowds: Why the Many Are Smarter Than the Few", 
				"James Surowiecki", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.DYNAMICS);
		
		// 11
		// Dynamics (Physical)
		insertBook("http://ecx.images-amazon.com/images/I/41FNH7T58FL._AA160_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/artificial_intelligence_coverscan.png", 
				context.getResources().getString(R.string.artificial_intelligence), 
				context.getResources().getString(R.string.artificial_intelligence_author), 
				"http://www.librarything.com/work/4108432/book/77792770", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://images.amazon.com/images/P/0262182629.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/processing_coverscan.png", 
				context.getResources().getString(R.string.processing), 
				context.getResources().getString(R.string.processing_author), 
				"http://www.librarything.com/work/3046748", ObjectType.BOOK, 
				Collection.INTERACTION);
		insertBook("http://images.amazon.com/images/P/0123736021.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/learning_processing_coverscan.png", 
				context.getResources().getString(R.string.learning_processing), 
				context.getResources().getString(R.string.learning_processing_author), 
				"http://www.librarything.com/work/5941512", ObjectType.BOOK, 
				Collection.DYNAMICS);
		insertBook("http://images.amazon.com/images/P/0262134748.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/designing_interactions_coverscan.png", 
				context.getResources().getString(R.string.designing_interactions), 
				context.getResources().getString(R.string.designing_interactions_author), 
				"http://www.librarything.com/work/2319884", ObjectType.BOOK, 
				Collection.INTERACTION);
		// Dynamics (Virtual)
		insertBook("http://bks8.books.google.co.uk/books?id=iTsZ5cg7gegC&printsec=frontcover&img=1&zoom=1", 
				context.getResources().getString(R.string.gestural_interfaces), 
				context.getResources().getString(R.string.gestural_interfaces_author), 
				"http://www.librarything.com/work/6739134", ObjectType.BOOK, 
				Collection.INTERACTION);
		insertBook("http://images.amazon.com/images/P/0123748992.01._SX140_SY225_SCLZZZZZZZ_.jpg", 
				context.getResources().getString(R.string.smart_things), 
				context.getResources().getString(R.string.smart_things_author), 
				"http://www.librarything.com/work/9889741", ObjectType.BOOK, 
				Collection.DYNAMICS);
		// Additionals
		insertBook("http://images.amazon.com/images/P/0262014866.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Living with Complexity", 
				"Donald A. Norman", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://images.amazon.com/images/P/0937073806.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Literate Programming", 
				"Donald E. Knuth", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://pics.librarything.com/picsizes/04/3b/043b45cadc468e759786c685977444941506f41.jpg", 
				"Voices of Time", 
				"J. G. Ballard", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INTERACTION);
		insertBook("http://images.amazon.com/images/P/0596529325.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Programming Collective Intelligence", 
				"Toby Segaran", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://images.amazon.com/images/P/1449381650.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Tapworthy", 
				"Josh Clark", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.DYNAMICS);
		
		// 11
		// Interaction (Physical)
 		insertBook("http://ecx.images-amazon.com/images/I/51bu0YG5ezL._SX225_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/interactivity_coverscan.png", 
				context.getResources().getString(R.string.programming_interactivity), 
				context.getResources().getString(R.string.programming_interactivity_author),
				"http://www.librarything.com/work/4108432/book/77792770", ObjectType.BOOK, 
				Collection.INFERENCE);
 		insertBook("http://images.amazon.com/images/P/1449383262.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/android_apps_coverscan", 
 				context.getResources().getString(R.string.android_apps), 
 				context.getResources().getString(R.string.android_apps_author), 
 				"http://www.librarything.com/work/book/77792748", ObjectType.BOOK, 
 				Collection.INTERACTION);
		insertBook("http://ecx.images-amazon.com/images/I/512J8000JML._SL500_AA300_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/user_interface_coverscan.png", 
				context.getResources().getString(R.string.user_interface), 
				context.getResources().getString(R.string.user_interface_author), 
				"http://www.librarything.com/work/4108432/book/77792770", ObjectType.BOOK, 
				Collection.INTERACTION);
		insertBook("http://images.amazon.com/images/P/0071385142.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/mobile_usability_coverscan.png", 
				context.getResources().getString(R.string.mobile_usability), 
				context.getResources().getString(R.string.mobile_usability_author), 
				"http://www.librarything.com/work/124375", ObjectType.BOOK, 
				Collection.DYNAMICS);
		insertBook("http://images.amazon.com/images/P/0596155441.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/mobile_design_development_coverscan.png", 
				context.getResources().getString(R.string.mobile_design_development), 
				context.getResources().getString(R.string.mobile_design_development_author), 
				"http://www.librarything.com/work/8855655", ObjectType.BOOK, 
				Collection.INFERENCE);
		insertBook("http://images.amazon.com/images/P/0470090898.01._SX140_SY225_SCLZZZZZZZ_.jpg",//"http://www.cip.ifi.lmu.de/~koelle/mobile_interaction_design_coverscan.png", 
				context.getResources().getString(R.string.mobile_interaction_design), 
				context.getResources().getString(R.string.mobile_interaction_design_author), 
				"http://www.librarything.com/work/1244257", ObjectType.BOOK, 
				Collection.INTERACTION);
		insertBook("http://wps.aw.com/wps/media/objects/1227/1257129/_skins_/D/default_vanilla/cover.jpg",//"http://www.cip.ifi.lmu.de/~koelle/designing_the_user_interface_coverscan.png", 
				context.getResources().getString(R.string.designing_the_user_interface), 
				context.getResources().getString(R.string.designing_the_user_interface_author), 
				"http://wps.aw.com/aw_shneider_dtui_4/", ObjectType.BOOK, 
				Collection.INFERENCE);
		// Interaction (Virtual)
		insertBook("http://images.amazon.com/images/P/0123708966.01._SX140_SY225_SCLZZZZZZZ_.jpg", 
				context.getResources().getString(R.string.visual_thinking), 
				context.getResources().getString(R.string.visual_thinking_author), 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INTERACTION);
		// Additionals
		insertBook("http://images.amazon.com/images/P/1846143411.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"You are Not a Gadget: A Manifesto", 
				"Jaron Lanier", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INTERACTION);	
		insertBook("http://images.amazon.com/images/P/0805840141.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Pointing: Where Language, Culture and Cognition Meet", 
				"Sotaro Kita", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.DYNAMICS);
		insertBook("http://images.amazon.com/images/P/0321687299.01._SX200_SY250_SCLZZZZZZZ_.jpg", 
				"Introducing HTML5 (Voices That Matter)", 
				"Bruce Lawson", 
				"http://www.librarything.com/work/5287914/book/77792654", ObjectType.BOOK, 
				Collection.INTERACTION);
		
		
		Log.d(LOG, "Sample books set up");
		return this;
	}
 	
 	/**
 	 * Close the Database.
 	 */
	public void close() {
		dbHelper.close();
	}
	
	/** 
	 * Gets a list of all {@link BookshelfObject} in the database.
	 */
	public ArrayList<BookshelfObject> getAllObjects() {
		database = dbHelper.getReadableDatabase();
		ArrayList<BookshelfObject> result = new ArrayList<BookshelfObject>();
		int max = database.rawQuery("SELECT * FROM "+ DATABASE_TABLE +";", null).getCount();
		int counter = 0;
		while (counter <= max) {
			Cursor cursor = database.rawQuery("SELECT " + DatabaseFields.TITLE.getKey() + 
					"," + DatabaseFields.COLLECTION.getKey() + "," + DatabaseFields.DRAWABLE.getKey() + 
					"," + DatabaseFields.TYPE.getKey() + " FROM books WHERE " + 
					DatabaseFields.ROWID.getKey() + " ='" + counter + "';", null);
			if (cursor.moveToNext()) {
				ObjectType type = ObjectType.values()[cursor.getInt(3)];
				result.add(new BookshelfObject(String.valueOf(counter),	cursor.getString(0),
					Collection.values()[Integer.valueOf(cursor.getString(1))],
					cursor.getString(2), type));
			}
			cursor.close();
			counter++;
		}
		Log.d("LOG", "got " + result.size() + " objects from database");
		database.close();
		return result;
	}

	// TODO(marionkoelle): create a method to get all objects in a certain criteria.
	public ArrayList<BookshelfObject> getAllObjects (int collection){
		database = dbHelper.getReadableDatabase();
		ArrayList<BookshelfObject> result = new ArrayList<BookshelfObject>();
		int max = database.rawQuery("SELECT * FROM "+ DATABASE_TABLE +";", null).getCount();	
		int counter = 0;
	
		while (counter <= max){
			Cursor cursor = database.rawQuery("SELECT " + DatabaseFields.TITLE.getKey() + 
				"," + DatabaseFields.COLLECTION.getKey() + "," + DatabaseFields.DRAWABLE.getKey() +
				"," + DatabaseFields.TYPE.getKey() + " FROM books WHERE " + 
				DatabaseFields.ROWID.getKey() + " ='" + counter + "' AND " + 
				DatabaseFields.COLLECTION.getKey() + " ='" + collection + "';", null);
			if (cursor.moveToNext()) {
				ObjectType type = ObjectType.values()[cursor.getInt(3)];
				result.add(new BookshelfObject(String.valueOf(counter),	cursor.getString(0), 
						Collection.values()[Integer.valueOf(cursor.getString(1))], 
						cursor.getString(2), type));
			} 
			cursor.close();
			counter++;
		}
		database.close();
		return result;
	}
	
	/**
	 * Gets the URL of a book from it's Book Id. 
	 */
	public String getURLforID (int id){
		database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery("SELECT " + DatabaseFields.URL.getKey() + " FROM " + 
			DATABASE_TABLE + " WHERE " + DatabaseFields.ROWID.getKey() + " ='" + id + "';", 
			null);
		// TODO(norrielm): Store this default value as a constant somewhere and remove it from this 
		// method. Should also maybe throw an exception and allow the application to use the default 
		// value. 
		String result = "http://www.librarything.com";
		if (cursor.moveToNext()) {
			result = cursor.getString(0);
		} 
		cursor.close();		
		database.close();
		return result;
	}
	
	/**
	 * Get the Type of a book given it's book id. 
	 */
	public int getTypeforID(int id) {
		database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery("SELECT " + DatabaseFields.TYPE.getKey() + " FROM " 
			+ DATABASE_TABLE + " WHERE " + DatabaseFields.ROWID.getKey() + " ='" + id + "';", 
			null);
		// TODO(norrielm): Find out why this is the default value and when an object will not have 
		// this value. Should also throw an excetion and remove this from this method.
		int result = 1;
		if (cursor.moveToNext()) {
			result = cursor.getInt(0);
		}
		cursor.close();		
		database.close();
		return result;
	}
	
	public String getTitleforID(String id) {
		database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery("SELECT " + DatabaseFields.TITLE.getKey()  + 
			" FROM " + DATABASE_TABLE + " WHERE " + DatabaseFields.ROWID.getKey() + " ='" + 
			id + "';", null);
		// TODO(norrielm): Again, as above with default values.
		String result = "Title";
		
		if (cursor.moveToNext()) {
			result = cursor.getString(0);
		}
		cursor.close();		
		database.close();
		return result;
	}
	
	public String getAuthorforID(String id) {
		database = dbHelper.getReadableDatabase();		
		Cursor cursor = database.rawQuery("SELECT " + DatabaseFields.AUTHOR.getKey() + " FROM "
			+ DATABASE_TABLE + " WHERE " + DatabaseFields.ROWID.getKey() + " ='" + id + "';", 
			null);
		// default value
		String result = "Author";
		
		if(cursor.moveToNext()){
			result = cursor.getString(0);
		}
		cursor.close();		
		database.close();
		return result;
	}	
	
	/**
	 * Get the collection id for a book or 0 if not found.
	 */
	// TODO(norrielm): Throw an exception if not found instead.
	public Collection getCollectionforID(String id) {
		database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery("SELECT " + DatabaseFields.COLLECTION.getKey() + 
			" FROM " + DATABASE_TABLE + " WHERE " + DatabaseFields.ROWID.getKey() + " ='" + id 
			+ "';", null);
		// default value
		int result = 0;
		if (cursor.moveToNext()) {
			result = cursor.getInt(0);	
		}
		cursor.close();		
		database.close();
		return Collection.values()[result];
	}
	
	/**
	 * Insert a new book into database. If it has been inserted before then this method does 
	 * nothing.
	 */
	// TODO(norrielm): Throw an exception if this has been inserted already or delete if unused.
	private void insertBook (String drawable, String title, String author, String url, 
			ObjectType type, Collection collection) {
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		Cursor cursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE " + 
			DatabaseFields.DRAWABLE.getKey() + " ='" + drawable + "' OR " + 
			DatabaseFields.TITLE.getKey() + " ='" + title + "';", null);
		if (!cursor.moveToNext()) {
			ContentValues values = new ContentValues();
		    values.put("drawable", drawable);
		    values.put(DatabaseFields.TITLE.getKey(), title);
		    values.put(DatabaseFields.AUTHOR.getKey(), author);
		    values.put(DatabaseFields.URL.getKey(), url);
		    values.put(DatabaseFields.TYPE.getKey(), type.ordinal());
			// TODO(norrielm): remove after debugging.
			Log.d(LOG, "book stored with collection : " + collection.ordinal() + 
					" and will be matched with collection " + 
					Collection.values()[collection.ordinal()]);
		    values.put(DatabaseFields.COLLECTION.getKey(), collection.ordinal());
			database.insert("books", null, values);
		}
		cursor.close();
		database.close();
	}

	public void moveToCollection(int id, Collection collection) {
		database = dbHelper.getWritableDatabase();
		ContentValues dataToInsert = new ContentValues();                          
		dataToInsert.put(DatabaseFields.COLLECTION.getKey(), collection.ordinal());
		String where = "_id=?";
		// TODO(norrielm): Find out whether we can change this method to accept a string as an arg. 
		// seeing as we don't use it as an int.
		String[] whereArgs = {Integer.toString(id)};

		database.update(DATABASE_TABLE, dataToInsert, where, whereArgs);	
		database.close();
	}
}
